<?php

	abstract class bCache {
		const NULL				="\x4\x3\x7";
		const RETURN_BUFFER 	='[return][buffer][result]';
		const RETURN_PASSED		='[return][passed][result]';
		const RETURN_FUNCTION 	='[return][function][result]';
		
		protected $sType;
		protected $mCache;
		protected $aConfiguration;
		
		public function Configure(array $aOption) {
			
			foreach($aOption as $sKey =>$sValue) {
				
				$this->$aConfiguration[$sKey] =$sValue;
			}
			
			return $this;
		}
		
		public function Open() {
			
			$aArgument =func_get_args();
			$bResult =(bool)call_user_func_array(array($this, $this->sType.'_open'), $aArgument);
			
			if($bResult) { ob_start(); }
			return $bResult;
		}
		
		public function Close($mCache =bCache::NULL) {
			
			$aArgument =func_get_args();
			$mBufferData =ob_get_contents(); ob_end_clean();
			
			if(!isset($this->aConfiguration['return_type'])) {
				
				if(in_array(($this->sType.'_close'), get_class_methods($this))) {
					
					$this->aConfiguration['return_type'] =bCache::RETURN_FUNCTION;
				} else {
					
					$this->aConfiguration['return_type'] =bCache::RETURN_PASSED;
				}
			}
			
			switch($this->aConfiguration['return_type']) {
				
				case bCache::RETURN_BUFFER:
					$this->mCache =$mBufferData;
				break;
				
				case bCache::RETURN_FUNCTION:
					if(in_array(($sTypeClose =$this->sType.'_close'), get_class_methods($this))) {

						$this->mCache =call_user_func_array(array($this, $sTypeClose), $aArgument);
					} else {
						
						$this->mCache ='return expected from a function!'; // should throw exception.
					}
				break;
				
				case bCache::RETURN_PASSED:
					$this->mCache =$mCache;
				break;
			}
			
			return $this;
		}
		
		public function Result() {
			
			return $this->mCache;
		}
		
		public function Type($sType) {
			
			$this->sType =$sType;
			return $this;
		}
		
		protected function SetResult($mData) {
			
			$this->mCache =$mData;
			return $this;
		}
		
		protected function FileTimeHash($sFilePattern) {
			
			$sTime ='';
			$aPattern =glob($sFilePattern);
			$aPattern =is_array($aPattern) ? $aPattern : array();
			
			foreach($aPattern as $sPathFile) {
				
				$sTime .=(int)filemtime($sPathFile);
			}
			
			return md5($sTime);
		}
	}

?>